package edu.unc.comp410.colemangeer.data;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Before;
import org.junit.Test;
import org.omg.CORBA.portable.ApplicationException;

import edu.unc.comp410.colemangeer.model.DataProvider;
import edu.unc.comp410.colemangeer.model.Product;

public class ProductGatewayCacheTest extends TestCase{
	DataProvider dataProvider= new DataProvider();
	IProductGateway productData;
	ProductGateway productGateway;
	Mockery context;
	IProductCache mockCache;
	
	@Before
	public void setUp(){
		try{
			dataProvider= new DataProvider();
			productData = dataProvider.getProductGateway();
			productGateway = (ProductGateway) productData;	
		}
		catch(ClassCastException e){
			System.err.println("Cache Test: Failed to get ProductGateway");
			e.printStackTrace();
			System.exit(-1);
		} 
		catch (ApplicationException e) {
			System.err.println("Cache Test: Failed to get ProductGateway");
			e.printStackTrace();
			System.exit(-1);
		}
		context = new Mockery();
		mockCache = context.mock(IProductCache.class);

		
	}

	@Test
	public void testOK(){
		Assert.assertEquals(true,true);
	}
	
	@Test
	public void testOneExpectation(){
		
		final Integer testId = Integer.valueOf(17);
		final Product prod17 = productGateway.getByID(testId);
		
		productGateway.setCache(mockCache);
		
		 // expectations
        context.checking(new Expectations() {{
            oneOf (mockCache).getProductById(testId); will(returnValue(null));
            oneOf (mockCache).addProduct(prod17);
            oneOf (mockCache).getProductById(testId); will(returnValue(prod17));
        }});

        //test
        Product p  = productGateway.getByID(testId); //the first two expectations
        p  = productGateway.getByID(testId);		 // the third expectation
        
        
        //verify
        context.assertIsSatisfied();
		
	}
	
	
	
	
}
